/******************************************************************
 * Copyright (c) 2007 - 2007, Simon Guerard, All rights reserved.
 * 
 * Projet de jeu de course au large.
 * 
 * This File is part of Sail-Race
 * 
 * Sail-Race is free software; you can redistribute it and/or modify it under the
 * terms of the GNU General Public License as published by the Free Software
 * Foundation; either version 2 of the License, or (at your option) any later
 * version.
 * 
 * Sail-Race is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * Sail-Race; if not, write to the Free Software Foundation, Inc., 51 Franklin St,
 * Fifth Floor, Boston, MA 02110-1301 USA
 */
package net.simcafamily.sailrace.vues;

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.annotation.Resource;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import net.simcafamily.sailrace.bo.Race;
import net.simcafamily.sailrace.core.AbstractView;
import net.simcafamily.sailrace.dao.IRaceDao;

import com.jgoodies.forms.factories.FormFactory;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.RowSpec;

@org.springframework.stereotype.Component("vueRacesList")
public class VueRacesList extends AbstractView {
    JPanel contentPane;

    @Resource(name = "raceDao")
    private IRaceDao raceDao;

    // affiche
    /**
     * @wbp.parser.entryPoint
     */
    @Override
    public Component buildContentPane() {
	// contruct the main panel of the view
	contentPane = new JPanel();
	// The layout contains 3 columns : the races'table, a separation and the
	// return button.
	contentPane.setLayout(new FormLayout(new ColumnSpec[] {
		ColumnSpec.decode("default:grow"), ColumnSpec.decode("10px"),
		ColumnSpec.decode("150px:grow"), },
		new RowSpec[] { FormFactory.DEFAULT_ROWSPEC, }));

	final List<Race> lstRaces = raceDao.getLstRaces();

	final JTable table = new JTable();
	final DefaultTableModel model = new DefaultTableModel(lstRaces.size(),
		3);
	int idxRace = 0;
	for (Race race : lstRaces) {
	    model.setValueAt(race.getName(), idxRace, 0);
	    model.setValueAt(race.getIcon(), idxRace, 1);
	    model.setValueAt("bouton", idxRace, 2);
	    idxRace++;
	}
	table.setModel(model);

	contentPane.add(table, "1, 1, fill, default");

	final JButton btnRetour = new JButton("Retour");
	btnRetour.addActionListener(new ActionListener() {
	    @Override
	    public void actionPerformed(ActionEvent event) {
		baseVue.setActionName("infosActionIntro");
		baseVue.getSynchro().set();
	    }
	});
	contentPane.add(btnRetour, "3, 1");

	return contentPane;
    }
}
